package com.xayy.mgt.controller;

import cn.kawins.base.basic.IBasicEndpoint;
import cn.kawins.base.result.ResultBody;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.mybatis.util.SnowflakeId;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xayy.mgt.model.NodeInfoRelation;
import com.xayy.mgt.model.NodeParamInfo;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.XmindResult;
import com.xayy.mgt.model.vo.XmindVo;
import com.xayy.mgt.service.NodeInfoRelationService;
import com.xayy.mgt.service.NodeInfoService;
import com.xayy.mgt.service.NodeParamInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/9/7 11:22
 */
@RestController
@RequestMapping("/xmind1")
@Api(tags = "将xmind文件转为卫星结构")
public class XmindController implements IBasicEndpoint {

    @Autowired
    private NodeInfoService nodeInfoService;
    @Autowired
    private NodeParamInfoService nodeParamInfoService;
    @Autowired
    private NodeInfoRelationService nodeInfoRelationService;

    @PostMapping("/insert")
    @ApiOperation(value = "插入数据")
    @Transactional
    public ResultBody xinmd(@RequestBody XmindVo xmindVo) {
        List<NodeTreeInfo> collect = new ArrayList<>();
        String xmindFile = xmindVo.getXmindFileName();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            XmindResult xmindResult = objectMapper.readValue(new File(xmindFile), XmindResult.class);
            Map<String, NodeTreeInfo> map = new HashMap<>();
            List<NodeParamInfo> nodeParamInfoList = new ArrayList<>();
            List<NodeInfoRelation> nodeInfoRelationList = new ArrayList<>();
            data(xmindVo.getParentId(), xmindResult, map, nodeParamInfoList, nodeInfoRelationList,
                xmindResult.getTitle());
            collect = new ArrayList<>(map.values());
            nodeInfoService.insertList(collect);
            nodeParamInfoService.insertList(nodeParamInfoList);
            nodeInfoRelationService.insertList(nodeInfoRelationList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return ResultBody.success(collect);
    }

    private void data(Long parentId, XmindResult xmindResult, Map<String, NodeTreeInfo> map,
        List<NodeParamInfo> nodeParamInfoList, List<NodeInfoRelation> nodeInfoRelationList, String parentName) {
        NodeTreeInfo parent = map.get(parentName);
        if (CollUtil.isNotEmpty(xmindResult.getTopics())) {
            // 节点
            Long id = new SnowflakeId().genId("node_tree_info", "id");
            NodeTreeInfo nodeTreeInfo = new NodeTreeInfo();
            nodeTreeInfo.setId(id);

            if (parent == null) {
                nodeTreeInfo.setParentId(parentId);
                nodeTreeInfo.setLevel(0);
                nodeTreeInfo.setSatelliteId(id);
            } else {
                nodeTreeInfo.setParentId(parent.getId());
                nodeTreeInfo.setLevel(parent.getLevel() + 1);
                nodeTreeInfo.setSatelliteId(parent.getSatelliteId());
                NodeInfoRelation nodeInfoRelation = new NodeInfoRelation();
                Long reId = new SnowflakeId().genId("t_node_info_relation", "id");
                nodeInfoRelation.setId(reId);
                nodeInfoRelation.setSatelliteId(parent.getSatelliteId());
                nodeInfoRelation.setFrom(parent.getId());
                nodeInfoRelation.setTo(id);
                nodeInfoRelation.setCreatedTime(new Date());
                nodeInfoRelationList.add(nodeInfoRelation);
            }
            if (nodeTreeInfo.getLevel() == 2) {
                nodeTreeInfo.setSystemId(parent.getId());
            } else if (nodeTreeInfo.getLevel() > 2) {
                nodeTreeInfo.setSystemId(parent.getSystemId());
            } else {
                nodeTreeInfo.setSystemId(null);
            }
            nodeTreeInfo.setNodeName(xmindResult.getTitle());
            nodeTreeInfo.setNodeCode(xmindResult.getCode());
            nodeTreeInfo.setColor("#00c7ef");
            map.put(xmindResult.getTitle(), nodeTreeInfo);
            List<XmindResult> topics = xmindResult.getTopics();
            topics
                .forEach(x -> data(parentId, x, map, nodeParamInfoList, nodeInfoRelationList, xmindResult.getTitle()));
        } else {
            // 参数
            // 节点
            Long id = new SnowflakeId().genId("t_node_param_info", "id");
            NodeParamInfo nodeParamInfo = new NodeParamInfo();
            nodeParamInfo.setId(id);
            nodeParamInfo.setSatelliteId(parent.getSatelliteId());
            nodeParamInfo.setNodeId(parent.getId());
            nodeParamInfo.setName(xmindResult.getTitle());
            nodeParamInfo.setCode(xmindResult.getCode());
            nodeParamInfo.setCreatedTime(new Date());
            nodeParamInfoList.add(nodeParamInfo);
        }
    }

}
